package com.zl.customer.web;

import com.zl.customer.pojo.Customer;
import com.zl.customer.pojo.Page;
import com.zl.customer.service.CustomerService;
import com.zl.customer.service.impl.CustomerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 分页查询的servlet
 * @author suke
 * @create 2021-07-07 10:28
 */
@WebServlet(name = "QueryByPageServlet",urlPatterns = "/QueryByPageServlet.action")
public class QueryByPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.设置编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //获取当前页码
        String pageIndexStr = request.getParameter("pageIndex");
        int pageIndex = 1;
        if(pageIndexStr != null && !pageIndexStr.isEmpty() ){
            pageIndex = Integer.parseInt(pageIndexStr);
        }
        //获取页容量
        int pageSize = 3;
        String pageSizeStr = request.getParameter("pageSize");
        if(pageSizeStr != null && !pageSizeStr.isEmpty() ){
            pageSize = Integer.parseInt(pageSizeStr);
        }

        //2.业务逻辑代码
        CustomerService  customerService = new CustomerServiceImpl();

        //3.调用方法
        Page<Customer> customerPage = customerService.queryByPage(pageIndex, pageSize);

        //把数据保存在request域
        request.setAttribute("pages",customerPage);

        //4.转发到list.jsp  使用绝对路径
        request.getRequestDispatcher("/jsp/list2.jsp").forward(request,response);


    }
}
